import type {
  AddSignRequest,
  ApprovalResponse,
  ApprovalTaskListResponse,
  ApproveRequest,
  BusinessLog,
  BusinessStageNode,
  CompletedTaskPageResponse,
  CopyPageRequest,
  CopyPageResponse,
  DelegateRequest,
  MyProcessPageRequest,
  MyProcessPageResponse,
  ProcessDefinition,
  ProcessInstance,
  RejectRequest,
  TaskPageRequest,
  TaskPageResponse,
  TodoQueryResponse,
  TransferRequest,
} from './types/workflow'
import type { ApprovalProcessDetail } from '@/types/api'
import { http } from '@/http/http'

/**
 * 获取快捷消息
 * @returns 快捷消息列表
 */
export function getTodoList() {
  return http.get<TodoQueryResponse>('/yngy-base/todo/simpleQuery', {
    t: Date.now(),
  })
}

/**
 * 获取待审批列表
 * @param data 请求参数
 * @returns 待审批列表
 */
export function getTodoTaskPage(data: TaskPageRequest) {
  return http.post<TaskPageResponse>('/yngy-flowable/simplify/todoPage', data, {
    t: Date.now(),
  })
}

/**
 * 获取审批节点
 * @param processInstanceId 流程实例ID
 * @returns 审批节点列表
 */
export function getTaskByProcessInstanceId(processInstanceId: string) {
  return http.get<ApprovalProcessDetail>(`/yngy-flowable/simplify/processFlow/${processInstanceId}`, {
    t: Date.now(),
  })
}

/**
 * 获取审批发起节点
 * @param id 流程实例ID
 * @returns 流程实例信息
 */
export function getProcessInstance(id: string) {
  return http.get<ProcessInstance>('/yngy-flowable/bpm/process-instance/get', {
    id,
  })
}

/**
 * 获取系统业务日志
 * @param data 请求参数
 * @returns 业务日志列表
 */
export function getSysActBusinessLogByList(data: { activitCode: string, businessId: string, timestamp: number }) {
  return http.post<BusinessLog[]>('/yngy-logistics/logistics/actBusinessLog/getSysActBusinessLogByList', data, {
    t: Date.now(),
  })
}

/**
 * 获取业务流程图节点
 * @param params 查询参数
 * @returns 业务流程图节点列表
 */
export function getBusinessStageNodes(params?: Record<string, any>) {
  return http.get<BusinessStageNode[]>('/yngy-base/workflowBusinessStage/getBusinessStageNodes', {
    ...params,
    t: Date.now(),
  })
}

/**
 * 获取流程定义
 * @param params 查询参数
 * @returns 流程定义信息
 */
export function getProcessDefinition(params?: Record<string, any>) {
  return http.get<ProcessDefinition>('/yngy-flowable/bpm/process-definition/get', {
    ...params,
    t: Date.now(),
  })
}

/**
 * 审批同意操作
 * @param data 请求参数
 * @returns 审批结果
 */
export function approveTask(data: ApproveRequest) {
  return http.post<ApprovalResponse>('/yngy-flowable/bpm/task/approve', data, {
    t: Date.now(),
  })
}

/**
 * 审批拒绝操作
 * @param data 请求参数
 * @returns 审批结果
 */
export function rejectTask(data: RejectRequest) {
  return http.post<ApprovalResponse>('/yngy-flowable/bpm/task/reject', data, {
    t: Date.now(),
  })
}

/**
 * 审批委派操作
 * @param data 请求参数
 * @returns 委派结果
 */
export function delegateTask(data: DelegateRequest) {
  return http.post<ApprovalResponse>('/yngy-flowable/bpm/task/delegate', data, {
    t: Date.now(),
  })
}

/**
 * 审批转派操作
 * @param data 请求参数
 * @returns 转派结果
 */
export function transferTask(data: TransferRequest) {
  return http.post<ApprovalResponse>('/yngy-flowable/bpm/task/transfer', data, {
    t: Date.now(),
  })
}

/**
 * 审批加签操作
 * @param data 请求参数
 * @returns 加签结果
 */
export function addSignTask(data: AddSignRequest) {
  return http.post<ApprovalResponse>('/yngy-flowable/bpm/task/create-sign', data, {
    t: Date.now(),
  })
}

/**
 * 获取已完成任务列表
 * @param params 请求参数
 * @returns 已完成任务列表
 */
export function getCompletedTaskPage(params: TaskPageRequest) {
  return http.post<CompletedTaskPageResponse>('/yngy-flowable/simplify/donePage', {
    ...params,
    t: Date.now(),
  })
}

/**
 * 获取抄送列表
 * @param params 请求参数
 * @returns 抄送列表
 */
export function getCopyPage(params: CopyPageRequest) {
  return http.get<CopyPageResponse>('/yngy-flowable/bpm/process-instance/copy/page', {
    ...params,
    t: Date.now(),
  })
}

/**
 * 获取我发起的列表
 * @param params 请求参数
 * @returns 我发起的列表
 */
export function getMyProcessPage(params: MyProcessPageRequest) {
  return http.post<MyProcessPageResponse>('/yngy-flowable/simplify/myPage', {
    ...params,
    t: Date.now(),
  })
}

/**
 * 获取待办数量
 * @returns 待办数量信息
 */
export function getTodoCount() {
  return http.get<number | string | null>('/yngy-base/todo/todoCount', {
    onlyApproval: true,
    t: Date.now(),
  })
}

/**
 * 获取审批信息列表
 * @param processInstanceId 流程实例ID
 * @returns 审批信息列表
 */
export function getApprovalTaskList(processInstanceId: string) {
  return http.get<ApprovalTaskListResponse>('/yngy-flowable/bpm/task/list-by-process-instance-id', {
    processInstanceId,
    t: Date.now(),
  })
}

// 导出默认对象，保持向后兼容
export default {
  getTodoList,
  getTodoTaskPage,
  getTaskByProcessInstanceId,
  getProcessInstance,
  getSysActBusinessLogByList,
  getBusinessStageNodes,
  getProcessDefinition,
  approveTask,
  rejectTask,
  delegateTask,
  transferTask,
  addSignTask,
  getCompletedTaskPage,
  getCopyPage,
  getMyProcessPage,
  getTodoCount,
  getApprovalTaskList,
}
